﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ResourceBlender.Core;

namespace ResourceBlender.DAL {
    public partial class ResourceBlenderBLL {
        /// <summary>
        /// Checks each column for differences and shows a warning if resources are not identical.
        /// </summary>
        /// <param name="resourceTable">The resource table.</param>
        /// <param name="colsToSkip">The number of columns to skip.</param>
        /// <returns>
        /// 	A collection of codes for languages in which the resources conflict.
        /// </returns>
        public static IEnumerable<string> DoResourcesConflict(DataTable resourceTable, int colsToSkip) {
            if(resourceTable == null)
                throw new ResourceBlenderException("Resource table cannot be null");
            List<string> conflictingLanguages = new List<string>();
            for(int col = colsToSkip; col < resourceTable.Columns.Count; col++) {
                string firstTranslation = string.Empty;
                for(int row = 0; row < resourceTable.Rows.Count; row++) {
                    // for each column (language) check whether there are any rows with differing translations
                    string translationForRow = (resourceTable.Rows[row][col] ?? string.Empty).ToString();
                    if(translationForRow.Length == 0)
                        continue;
                    if(firstTranslation.Length == 0) { // this is the first resource with a translation for the current column
                        firstTranslation = translationForRow;
                    } else if(translationForRow != firstTranslation) {
                        conflictingLanguages.Add(resourceTable.Columns[col].ColumnName);
                        break;
                    }
                }
            }
            return conflictingLanguages;
        }
    }
}
